package com.xu.sms.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xu.sms.R;
import com.xu.sms.controller.FBOInfoActivity;
import com.xu.sms.entity.Info;
import com.xu.sms.utils.HtmlUtil;
import com.xu.sms.utils.LogUtil;
import com.xu.sms.utils.PageUtil;

public class FBOInfoAdapter extends BaseAdapter {

	private String TAG = FBOInfoAdapter.class.getSimpleName();
	private Context context;
	private List<Info> infoList;
	private PageUtil pageUtil;
	private Handler handler;
	private String keyword;
	private Integer typeId;

	public FBOInfoAdapter() {
	};

	public FBOInfoAdapter(Context context, List<Info> infoList,Handler handler
			,String keyword,Integer typeId) {
		this.context = context;
		this.infoList = infoList;
		this.keyword = keyword;
		this.typeId = typeId;
		this.handler = handler;
		pageUtil = new PageUtil(1, infoList.size());
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return infoList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return infoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.WRAP_CONTENT,
			LinearLayout.LayoutParams.WRAP_CONTENT);

	// TextView item_fboinfo_tv_curPage;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		if (convertView != null) {
			view = convertView;
		} else {
			view = View.inflate(context, R.layout.listview_item_fboinfo, null);
		}
		
		TextView item_fboinfo_tv_content = (TextView) view
				.findViewById(R.id.item_fboinfo_tv_content);
		TextView item_fboinfo_tv_curPage = (TextView) view
				.findViewById(R.id.item_fboinfo_tv_curPage);
		TextView item_fboinfo_tv_sendmsg = (TextView) view
				.findViewById(R.id.item_fboinfo_tv_sendmsg);
		setPageNum(position, item_fboinfo_tv_curPage);
		Info info = infoList.get(position);
		//设置单击事件
		item_fboinfo_tv_sendmsg.setOnClickListener(new MyOnClickListener(info.getInfoContent()));
LogUtil.i(TAG, 89, this.keyword);	
		if(keyword !=null){
		   String _content = HtmlUtil.setSelectKeywordColor(info.getInfoContent(), keyword, "#E21918");

		   item_fboinfo_tv_content.setText(Html.fromHtml(_content));
		}else{
			item_fboinfo_tv_content.setText(info.getInfoContent());
		}
		return view;
	}

	private void setPageNum(int position, TextView item_fboinfo_tv_curPage) {
		// 显示页数： marginTop,marginBottom |2dp 2dp fontSize 15sp visible
		// 不显示页数：fontSize 6sp invisible
		int pageCur = getPageCur(position, pageUtil.getPageNum(),
				pageUtil.getTotoalPage());
		if (pageCur == -1) {
			item_fboinfo_tv_curPage.setTextSize(6);
			item_fboinfo_tv_curPage.setVisibility(TextView.INVISIBLE);
			lp.setMargins(0, 0, 0, 0);
		} else {
			item_fboinfo_tv_curPage.setTextSize(15);
			item_fboinfo_tv_curPage.setVisibility(TextView.VISIBLE);
			item_fboinfo_tv_curPage.setText("第" + pageCur + "页");
			// 左上右下
			lp.setMargins(0, 5, 0, 5);
		}
		item_fboinfo_tv_curPage.setLayoutParams(lp);
	}

	// 判断你是一个数在一个5倍数的那个范围
	public static int getPageCur(int position, int pageNum, int totalPage) {
		position += 1;
		int temp = 0;
		if (position == 1) {
			return 1;
		}
		int i = 1;
		for (; i <= totalPage; i++) {
			temp = i * pageNum;
			if (position == (temp + 1)) {
				return i + 1;// 页数
			}
		}
		return -1;
	}
	
	class MyOnClickListener implements OnClickListener{

		private String content;
		public MyOnClickListener(){};
		public MyOnClickListener(String content){
			this.content = content;
		};
		@Override
		public void onClick(View v) {
			Message msg = Message.obtain();
			msg.what = FBOInfoActivity.INFO_SEND_MSG_STATUS_CODE;
			msg.obj = content;
			handler.sendMessage(msg);
		}
		
	}

}
